const databaseConfig = require('../config/databaseConfig')
const mysql = require('mysql')
const { login } = require('../controller/userController')

const databasePool = {
    pool:'',
    createPool:function(){
        this.pool = mysql.createPool(databaseConfig)
    },
    query:function({sql,props}){
        return new Promise((resolve,reject) => {
            this.pool.getConnection((err,connection) => {
                this.pool.getConnection((err,connection)=>{
                    if(err){
                        reject('sql connect is wrong')
                    }else{
                        connection.query(sql,props,(err,data) => {
                            data && resolve(data)
                            err && reject(err)
                        })
                        connection.release();
                    }
                })
            })
        })
    }
}

databasePool.createPool()

module.exports = databasePool
